Ext.define('js.user.UserGroupList', {
    extend: 'Ext.grid.Panel',
    requires: ['js.Util', 'js.ctrl.Common'],
    controller: 'Common',
    displayField: 'group_name',
    id: 'js.user.UserGroupList',
    userGroupForm: function () {
        return Ext.create('js.user.UserGroupForm');
    },
    initComponent: function () {
        this.columns = [
            {text: '用户组名称', dataIndex: 'group_name', align: 'center', flex: 1}
        ];
        this.store = Ext.create('js.store.user.UserGroup');

        this.bbar = [{
            text: '添加',
            id:'userGroupAddButton',
            scope: this,
            handler: this.showAddForm,
            disabled:true,
        }, {
            text: '修改',
            scope: this,
            handler: this.showEditForm
        }, {
            text: '删除',
            scope: this,
            handler: this.deleteSelection
        }];

        this.callParent(arguments);
    },
    isSelectionValid: function (selection) {
        if (selection.length !== 1) {
            Ext.toast('请选择需要操作的用户组');
            return false;
        } else {
            let node = selection.shift().get('group_id');
            if (!node) {
                Ext.toast('根节点不能操作');
                return false;
            } else {
                return true;
            }
        }
    },
    showAddForm: function () {
        let win = util.window(this.userGroupForm(), {title: '添加'});
        win.show();
    },
    showEditForm: function () {
        if (this.isSelectionValid(this.getSelection())) {
            let formPanel = this.userGroupForm();
            let rec = this.getSelection().shift();
            formPanel.setValues(rec.data);
            let win = util.window(formPanel, {title: '修改'});
            win.show();
        }
    },
    deleteSelection: function () {
        if (this.isSelectionValid(this.getSelection())) {
            let me = this;
            Ext.Msg.confirm('提示', '确定要删除所选项吗？删除后所有从属对象也将被删除', function (ok) {
                if (ok === 'yes') {
                    util.prompt(function () {
                        let group_id = me.getSelection().shift().get('group_id');
                        util.get('userGroup', {
                            action_type: 'userGroupDel',
                            ids: group_id
                        }).then(function (response, opts) {
                            try {
                                let obj = Ext.decode(response.responseText);
                                if (obj.success) {
                                    Ext.toast('删除成功');
                                    me.getStore().reload();
                                } else {
                                    Ext.toast(obj.message);
                                }
                            } catch (e) {
                                Ext.toast(e.message);
                            }
                        }, function (response, opts) {
                            Ext.toast(`删除失败：${response.status}`);
                        })
                    });
                }
            });
        }
    }
});